package cn.jsj.dao;

import java.util.List;
import java.util.ListIterator;

import org.hibernate.criterion.Restrictions;

import cn.jsj.pojo.TblPayDetailInfo;
import cn.jsj.pojo.TblRoyaltyInfo;

/**
 * 付款详细情况DB操作类
 * @author LI
 *
 */
public class TblPayDetailDAO {

	/**
	 * 保存一条付款详情
	 * @param tpdi:付款详情类
	 * @return
	 */
	public int savePayDetialInfo(cn.jsj.pojo.TblPayDetailInfo tpdi){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		session.save(tpdi);
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}

	/**
	 * 根据合同号获取付款实际情况
	 * @param contractId：合同号
	 * @return：付款实际情况
	 */
	public String getPayDetail(String contractId){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblPayDetailInfo.class).add(
				Restrictions.eq("caseNumber",contractId))
				.list();
		// 提交事务
		tx.commit();
		session.close();
		//提成详情
		String payDetails="";
		for(int i=0;i<result.size();i++){
			TblPayDetailInfo tri=(TblPayDetailInfo)result.get(i);
			payDetails+=tri.getPaymentTime().toString().substring(0, 10)+": 付款"+tri.getPaymentMoney()+"元，";
		}
		return payDetails;
	}

	/**
	 * 根据合同号获取这份合同已付款总资金
	 * @param contractId：合同ID
	 * @return：已付款总金额
	 */
	public int getTotalPayMoney(String contractId){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select sum(paymentMoney) from TblPayDetailInfo where caseNumber=?";
	List result=	session.createQuery(hql)
		.setString(0, contractId).list();

		// 提交事务
		tx.commit();
		session.close();
		if(result==null||result.isEmpty()){
			return 0;
		}else{
			if(result.get(0)==null){
				return 0;
			}

			return Integer.parseInt(((Long)result.get(0)).toString());
		}
	}



	/**
	 * 获得某个律师在某段时间内他所有案件的实际付款总金额
	 * @param lpp：参数类
	 * @return：付款总金额
	 */
	public int getLawyerTotalMoney(cn.jsj.bgtodb.LawyerPerformanceProperty lpp){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select sum(p.paymentMoney) from TblPayDetailInfo p, TblCaseInfo c where p.caseNumber=c.contractNumber and c.undertakeLawyer=? and c.caseSignTime between?and?";
	List result=session.createQuery(hql)
		.setInteger(0, lpp.getLpLawyerId())
		.setDate(1, lpp.getLpstartTime())
		.setDate(2, lpp.getLpendTime())
		.list();

		// 提交事务
		tx.commit();
		session.close();
		if(result==null||result.isEmpty()){
			return 0;
		}else{
			if(result.get(0)==null){
				return 0;
			}

			return Integer.parseInt(((Long)result.get(0)).toString());
		}
	}


	/**
	 *  查看律师提成报表信息
	 * @param lcp：参数类
	 * @return：律师提成报表信息
	 */
	public List getPayDetailChart(cn.jsj.bgtodb.LawyerChartProperty lcp){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select c.undertakeLawyer, sum(p.paymentMoney) from TblPayDetailInfo p, TblCaseInfo c where p.caseNumber=c.contractNumber and c.caseSignTime between?and? group by c.undertakeLawyer";
		List result=session.createQuery(hql)
		.setDate(0, lcp.getStartTime())
		.setDate(1, lcp.getEndTime())
		.list();
		// 提交事务
		tx.commit();
		session.close();
		return result;
	}




	/**
	 * 获取案件类别报表中案件实际付款总金额
	 * @param ctcp：案件付款总金额参数类
	 * @return：案件付款金额
	 */
	public int getCaseTypeChartMoney(cn.jsj.bgtodb.CaseTypeChartProperty ctcp){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select sum(p.paymentMoney) from TblPayDetailInfo p, TblCaseInfo c where p.caseNumber=c.contractNumber and c.caseType=? and c.caseSignTime between?and?";
	List result=session.createQuery(hql)
		.setInteger(0, ctcp.getCaseTypeNumber())
		.setDate(1, ctcp.getStartTime())
		.setDate(2, ctcp.getEndTime())
		.list();

		// 提交事务
		tx.commit();
		session.close();
		if(result==null||result.isEmpty()){
			return 0;
		}else{
			if(result.get(0)==null){
				return 0;
			}

			return Integer.parseInt(((Long)result.get(0)).toString());
		}

	}

	/**
	 * 获得在某段时间内所业绩中的付款情况
	 * @param et :时间段参数类
	 * @return：付款情况
	 */
	public int getOfficePayMoney(cn.jsj.bgtodb.ExcerptTime et){

		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select sum(p.paymentMoney) from TblPayDetailInfo p where p.paymentTime between?and?";
	List result=session.createQuery(hql)
		.setDate(0, et.getStartTime())
		.setDate(1, et.getEndTime())
		.list();

		// 提交事务
		tx.commit();
		session.close();
		if(result==null||result.isEmpty()){
			return 0;
		}else{
			if(result.get(0)==null){
				return 0;
			}
			return Integer.parseInt(((Long)result.get(0)).toString());
		}
	}













}
